<?php

namespace App\Http\Controllers\Admin;

use App\Models\Kind;
use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
 * 分类管理
 * Class KindController
 * @package App\Http\Controllers\Admin
 */
class KindController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $list = Kind::paginate(config('site.other.paginate'));
        return backendView('kind.index', compact('list'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('post')) {

            $this->validate($request, [
                'name' => 'required|max:255',
                'status' => 'required|in:0,1'
            ], [], [
                'name' => __("Name"),
                'status' => __("Status")
            ]);

            $data = $request->only(['name', 'status']);

            if (Kind::create($data)) {
                return redirect()->route('admin.kind.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {

            return backendView('kind.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {
        /**
         * @var $kind Kind
         */
        $kind = Kind::find($id);
        if (empty($kind)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => 'required|max:255',
                'status' => 'required|in:0,1'
            ], [], [
                'name' => __("Name"),
                'status' => __("Status")
            ]);

            $data = $request->only(['name', 'status']);

            if ($kind->update($data)) {
                return redirect()->route('admin.kind.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('kind.edit', compact('kind'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $kind Kind
         */
        $kind = Kind::find($id);
        if (empty($kind)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('kind.view', compact('kind'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $kind Kind
         */
        $kind = Kind::find($id);
        if (empty($kind)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
/*            $users = $kind->users()->get();
            $devices = $kind->KindDevices()->get();
            //dd($games);
            Log::error($users);
            Log::error($devices);
            if (count($users) > 0 || count($devices) > 0) {
                return back()->with('error', '机构下有账户或设备，不能删除');
            }*/

            if ($kind->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            Log::error('Kind delete error:');
            Log::error($e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}